package com.rxt.thermopro.ui.dialog

import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.app.widget.PressTextView
import com.rxt.thermopro.R

class TypeBottomDialog : DialogFragment() {

    private lateinit var contentView: View
    private var isShow = false
    private var mScreenDensity = 0f
    private var mScreenHeight = 0
    private var mScreenWidth = 0
    private lateinit var cancelView: PressTextView
    private var mListener: OnTypeSelectListener? = null
    private lateinit var rangeTempVew:View
    private lateinit var targetTempVew:View

    companion object {
        const val TYPE_TARGET_TEMP = 1
        const val TYPE_RANGE_TEMP = 2
    }

    interface OnTypeSelectListener {
        fun onSelected(type: Int)
    }

    fun setOnTypeSelectListener(listener: OnTypeSelectListener) {
        mListener = listener
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireActivity())

        contentView = View.inflate(requireContext(), R.layout.dialog_type_bottom, null)
        initListener()

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(contentView)
        dialog.setCanceledOnTouchOutside(false)

        val displayMetrics = DisplayMetrics()
        requireActivity().windowManager.defaultDisplay.getMetrics(displayMetrics)
        mScreenDensity = displayMetrics.density
        mScreenHeight = displayMetrics.heightPixels
        mScreenWidth = displayMetrics.widthPixels

        isCancelable = true
        val window: Window? = dialog.window
        if (window != null) {
            window.setBackgroundDrawable(ColorDrawable(resources.getColor(android.R.color.transparent)))
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            val lp: WindowManager.LayoutParams = window.attributes
            lp.gravity = Gravity.BOTTOM
            lp.width = mScreenWidth
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT
            window.attributes = lp
        }
        return dialog
    }

    private fun initListener() {
        cancelView = contentView.findViewById(R.id.viewCancel)
        rangeTempVew=contentView.findViewById(R.id.rangeTempVew)
        targetTempVew=contentView.findViewById(R.id.targetTempVew)

        cancelView.setOnClickListener {
            hideDialog()
        }

        targetTempVew.setOnClickListener {
            mListener?.onSelected(TYPE_TARGET_TEMP)
            hideDialog()
        }

        rangeTempVew.setOnClickListener {
            mListener?.onSelected(TYPE_RANGE_TEMP)
            hideDialog()
        }

    }

    fun showDialog(fragmentManager: FragmentManager) {
        try {
            if (!isShow) {
                this.show(fragmentManager, "TAG")
                isShow = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("123->", "showDialog Exception $e")
        }
    }

    fun hideDialog() {
        if (isShow) {
            dismiss()
            isShow = false
        }
    }

    override fun onResume() {
        super.onResume()
        isShow = true
    }

    override fun onPause() {
        super.onPause()
        isShow = false
    }

    override fun show(manager: FragmentManager, tag: String?) {
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }
}